package com.chen.ssm.filter;


import javax.servlet.*;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class CookiesFilter implements Filter {


  public void destroy() {

  }


  public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain)
          throws IOException, ServletException {
    HttpServletRequest request = (HttpServletRequest) req;
    HttpServletResponse response = (HttpServletResponse) resp;
    System.out.println(request.getRequestURI());
    String requestURI = request.getRequestURI();
   /* if (requestURI.indexOf("index") > -1 || requestURI.indexOf("resources") > -1 || requestURI.indexOf("managerLoginPage") > -1 || requestURI.indexOf("manager/login") > -1) {
      chain.doFilter(request, response);
      return;
    }
    if (requestURI.indexOf("/flight/") > -1 || requestURI.indexOf("/ticketOrder/") > -1 || requestURI.indexOf("/aircraft/") > -1 || requestURI.indexOf("/user/") > -1) {
      chain.doFilter(request, response);
      return;
    }*/
    if (!(requestURI.indexOf("/manager/") > -1)) {
      chain.doFilter(request, response);
      return;
    } else {
      if (requestURI.indexOf("/manager/managerLoginPage") > -1 || requestURI.indexOf("/manager/login") > -1) {
        chain.doFilter(request, response);
        return;
      }
      if (request.getCookies() != null) {
        Cookie[] cs = request.getCookies();
        System.out.println(cs.length);
        if (cs != null && cs.length > 0) {
          for (Cookie c : cs) {
            String cName = c.getName();
            System.out.println(cName);
            if (cName.equals("adminToken")) {
              // String token = c.getValue();
              // String userAndPwd = new DesUtil().decrypt(token, key);
              // String[] uNameAndPwd = userAndPwd.split(value);
              //  String userName = uNameAndPwd[0];
              //request.getSession().setAttribute("userName", token);
              chain.doFilter(request, response);
              return;
            }
          }
        }
      }
    }
    response.sendRedirect("/MavenDemo/manager/managerLoginPage");
  }


  public void init(FilterConfig arg0) throws ServletException {

  }
}
